package com.jxtti.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.ArticleCategory;
import com.jxtti.entity.ArticleList;
import com.jxtti.service.ArticleCategoryService;
import com.jxtti.service.ArticleListService;

/**
 * 微网站入口.
 * 
 * @author Administrator
 * 
 */

@Controller
@RequestMapping("/MicroSite")
public class MicroSiteController {

	@Autowired
	private ArticleListService articleListService;

	
	@Autowired //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private ArticleCategoryService  articleCategoryService; 
	
	
	
	@RequestMapping(value = "/index.htm")
	@ResponseBody
	public ModelAndView index(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			//你级目录	
			mav.addObject("data", data);
			mav.setViewName("/pages/MicroSite/index.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	/**
	 * 路转到文章列表页.
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/listAicroSiteInterface")
	@ResponseBody
	public ModelAndView listAicroSiteInterface(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			//你级目录
			String id= data.get("categoryID").toString();
			//判断是否有下一级目录.
			Map<String , Object> m = new HashMap();
			m.put("parentID", id);
			List<ArticleCategory> lst = articleCategoryService.queryAll(m);
			mav.addObject("dataLength", lst.size());
			
			mav.addObject("data", data);
			mav.setViewName("/pages/MicroSite/articleList.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	
	
	/**
	 * 路转到文章列表页.
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/listMainArticleInterface")
	@ResponseBody
	public ModelAndView listMainArticleInterface(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			//你级目录
			String id= data.get("categoryID").toString();
			//判断是否有下一级目录.
			Map<String , Object> m = new HashMap();
			m.put("parentID", id);
			List<ArticleCategory> lst = articleCategoryService.queryAll(m);
			mav.addObject("dataLength", lst.size());
			
			mav.addObject("data", data);
			mav.setViewName("/pages/MicroSite/mainAricleList.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	/**
	 * 环境质量页面.
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/envQualityInterface")
	@ResponseBody
	public ModelAndView envQualityInterface(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/MicroSite/evn_quality.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	/**
	 * 模块查文章.
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryArticleByModel")
	@ResponseBody
	public Map<String, Object> queryArticle(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		PageInfo<ArticleList> page = articleListService.listPage(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());

		return result;
	}

	
	@RequestMapping("/getArticleCategoryByPage")
	@ResponseBody
	public Map<String, Object> getArticleCategoryByPage(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> result = new HashMap<String, Object>();
		PageInfo<ArticleCategory> page = this.articleCategoryService.listPage(data);
		
		result.put("total", page.getTotal());
		result.put("rows", page.getList());

		return result;
	}
	
	
	
	/**
	 * 查询文章内容页.
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detailArticleInterface")
	@ResponseBody
	public ModelAndView detailArticleInterface(
			@RequestParam Map<String, Object> data, ArticleList entity,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);

			entity.setId(Integer.parseInt(data.get("id").toString()));
			entity = this.articleListService.queryById(entity);
			entity.setContents(delTagsFContent(entity.getContents()));
			mav.addObject("articleList", entity);
			mav.setViewName("/pages/MicroSite/detailArticle.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	
	/**
	 * 去除<span> 中的样式。
	 * @param htmlStr
	 * @return
	 */
	public String delTagsFContent(String htmlStr) {
		
		  Pattern p_style = Pattern.compile("<span([^>]{0,})>", Pattern.CASE_INSENSITIVE);  
	        Matcher m_style = p_style.matcher(htmlStr);  
	        htmlStr = m_style.replaceAll("<span>"); // 过滤style标签  
		return htmlStr;
	}
	
	
	
	/**
	 * 根据根目录查询所有子目录 下的文章.
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/loadArticleByParentArticleCategory")
	@ResponseBody 
	public List<ArticleList> loadArticleByParentArticleCategory(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//data.put("categoryID", data.get("id"));
		
		return articleListService.cteTreeArticleListByCategoryID(data);
		
	}
	
	/**
	 *  根据根目录查询所有子目录 下的文章并进行前端分面.
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/loadArticleByParentArticleCategoryByPage")
	@ResponseBody 
	public Map<String, Object> loadArticleByParentArticleCategoryByPage(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		//data.put("categoryID", data.get("id"));
		
		
		Map<String, Object> result = new HashMap<String, Object>();
		PageInfo<ArticleList> page = this.articleListService.cteTreeArticleListByCategoryIDByPage(data);
		
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		
		
		return result;
		
	}
	
}
